package cn.erp.app.system.controller;

import java.util.ArrayList;
import java.util.List;

import cn.erp.app.service.BaseInfoService;
import cn.erp.app.service.MenuService;
import cn.erp.app.service.ProduceService;
import cn.erp.app.util.EuiRender;
import cn.erp.common.model.SvcBaseinfo;
import cn.erp.common.model.SvcMaterielSubdivision;
import cn.erp.common.model.SvcProduceCategory;
import cn.erp.common.model.SystemMenu;
import cn.erp.common.model.SystemUser;

import com.jfinal.common.controller.BaseController;
import com.jfinal.ext.ret.ReqResult;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;

@ControllerBind(controllerKey = "/system/menu", viewPath = "/system/menu")
public class MenuController extends BaseController {
	public void index() {
		setAttr("menus", getSession().getAttribute("menus"));
		render("menu_index.html");
	}

	public void getdata() {

		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("rows", 10);
		SystemMenu menu = getModel(SystemMenu.class, "menu");

		String where = "from system_menu where 1=1  ";
		List<Object> objs = new ArrayList<Object>();
		if (menu.getLevel() != null) {
			where += " and level = ? ";
			objs.add(menu.getLevel());
		}
		if (menu.getMenuName() != null) {
			where += " and  menuName = ? ";
			objs.add(menu.getMenuName());
		}
		System.out.println(objs.toArray());
		Page<SystemMenu> sysuser = SystemMenu.dao.paginate(pageNumber,
				pageSize, "select * ", where, objs.toArray());
		render(EuiRender.page(sysuser));
	}

	public void search() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("rows", 10);
		SystemMenu menu = getModel(SystemMenu.class, "menu");

		String where = "from system_menu where 1=1  ";
		List<Object> objs = new ArrayList<Object>();
		if (menu.getLevel() != null) {
			where += " and level = ? ";
			objs.add(menu.getLevel());
		}
		if (menu.getMenuName() != null) {
			where += " and  menuName = ? ";
			objs.add(menu.getMenuName());
		}
		System.out.println(objs.toArray());
		Page<SystemMenu> sysuser = SystemMenu.dao.paginate(pageNumber,
				pageSize, "select * ", where, objs.toArray());
		render(EuiRender.page(sysuser));
	}

	public void openadd() {
		long id= getParaToLong("id",0L);
		setAttr("menus", getSession().getAttribute("menus"));
		SystemMenu pojo = new SystemMenu();
		if(id!=0L) {
			pojo = SystemMenu.dao.findById(id);
			
		}
		setAttr("pojo",pojo);
		render("menu_add.html");
	}
	
	public void saveorupdate() {
		SystemMenu systemMenu = getModel(SystemMenu.class, "");
		try {
			if (systemMenu.getParentId() == 0) {// 表明是主菜单
				systemMenu.setLevel(0);
			} else {
				systemMenu.setLevel(1);
			}
			if (systemMenu.getId() != null && systemMenu.getId() != 0) {
				systemMenu.update();
			} else {
				systemMenu.save();
			}
			renderJson(ReqResult.ADD_SUCCESS);
			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.ADD_ERROR);
	}

	public void delete() {
		String[] ids = getParaValues("ids");
		try {
			if (ids != null && !ids.equals("")) {
				for (String id : ids) {
					SystemMenu.dao.deleteById(id);
				}
			}
			renderJson(ReqResult.DEL_SUCCESS);
			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.DEL_ERROR);

	}

	public void edit() {
		long id = getParaToLong("id");
		try {
			SystemMenu systemMenu = SystemMenu.dao.findById(id);
			renderJson(systemMenu);
			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.ADD_ERROR);
	}
}
